<template>
  <div class="app-container">
    <div class="custom_card_0">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" size="small">
        <el-form-item label="模板名称" prop="contractTemplateName">
          <el-input v-model="queryParams.contractTemplateName" placeholder="请输入模板名称" clearable
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="所属公司" prop="enterpriseCertificationName">
          <el-input v-model="queryParams.enterpriseCertificationName" placeholder="请输入所属公司" clearable
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="业务类型" prop="businessTypeList">
          <el-select v-model="queryParams.businessTypeList" placeholder="请选择业务分类" clearable>
            <el-option label="租车合同" :value="1"/>
            <el-option label="续租合同" :value="2"/>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="custom_card">
      <el-row class="mb8" :gutter="10">
        <el-col :span="3">
          <el-button class="first-plain-btn" size="medium" @click="addContractTemplete">新增模板</el-button>
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="handleQuery"
        ></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="basicList"
        stripe
        :header-cell-style="{ background: '#eef1f6', color: '#606266' }" border>
        <template slot="empty">
          <el-empty :image-size="50" description="暂无合同信息"></el-empty>
        </template>
        <el-table-column
          align="center"
          prop="contractTemplateName"
          label="模板名称"
          min-width="180">
        </el-table-column>
        <el-table-column
          align="center"
          prop="name"
          label="业务类型"
          min-width="180">
          <template v-slot="{row}">
            {{getListToString(row.businessTypeList)}}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="remark"
          label="备注"
          min-width="180">
        </el-table-column>
        <el-table-column
          align="center"
          prop="enterpriseCertificationName"
          label="所属企业"
          min-width="180">
        </el-table-column>
        <el-table-column
          align="center"
          label="状态"
          min-width="180">
          <template v-slot="{row}">
            {{row.status|templeteStatus}}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="operationName"
          label="创建人"
          min-width="180">
        </el-table-column>
        <el-table-column
          align="center"
          prop="createTime"
          label="创建时间"
          min-width="180">
        </el-table-column>

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed="left">
          <template v-slot="{ row }">
            <el-button size="small" type="text"
                       @click="handleOperate(1,row)">查看
            </el-button>
            <el-button size="small" type="text"
                       @click="handleOperate(2,row)">编辑
            </el-button>
            <el-button v-if="row.status==2" size="small" type="text"
                       @click="handleOperate(3,row)">启用
            </el-button>
            <el-button v-if="row.status==1" size="small" type="text"
                       @click="handleOperate(4,row)">禁用
            </el-button>


          </template>
        </el-table-column>
      </el-table>

      <pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                  @pagination="getList"/>
    </div>

  </div>
</template>

<script>

import {listTemplate, updateTemplateStatus} from "@/api/contract/template";

export default {
  name:'Template',
  components: {},
  props: {},
  data() {
    // 这里存放数据
    return { // 加载标识
      loading: false,
      showSearch: true,
      // 总条数
      total: 0,
      // 工单数据
      basicList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractTemplateName: '',
        enterpriseCertificationName: '',
        businessTypeList:[]
      },
    }
  },
  // 计算属性 类似于 data 概念
  computed: {},
  // 方法集合
  methods: {
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery(){
      this.$refs.queryForm.resetFields();
      this.handleQuery();
    },
    getList() {
      this.loading=true;
      listTemplate(this.queryParams).then(res=>{
        this.loading=false;
        this.total = res.total;
        this.basicList = res.rows;
      })
    },
    /*将数组转为字符换*/
    getListToString(list){
      let businessTypes = [];
      if (list!=null&&list.length>0){
        list.forEach((item)=>{
          if (item==1){
            businessTypes.push('租车合同');
          }else if(item==2){
            businessTypes.push('续租合同');
          }
        })
      }
      return businessTypes.toString();
    },

    /*新增合同模板*/
    addContractTemplete(){
      this.$router.push({name:'addContractTemplate', query:{type:0}})
    },
    /*操作按钮点击事件*/
    handleOperate(type, row) {
      const currentId = row.contractId;
      switch (type) {
        case 1://查看
          this.$router.push({path:'/contractTemplate/detail', query:{id:currentId}});
          break
        case 2://编辑
          this.$router.push({name:'addContractTemplate', query:{type:1, id:currentId}})
          break
        case 3://启用
          this.updateTempleteStatus(currentId, 1);
          break
        case 4://禁用
          this.updateTempleteStatus(currentId, 2);
          break

        default:
          console.log('未知')
          break
      }
    },
    /*修改模板状态*/
    updateTempleteStatus(contractId,status) {
      let msg = status==1?"启用成功":"禁用成功";
      this.loading = true;
      updateTemplateStatus({
        contractId : contractId,
        status : status
      }).then(res=>{
        this.loading = false;
        this.$message({
          type:'success',
          message:msg
        })
        this.handleQuery();
      }).catch(()=>{
        this.loading = false;
      })
    },
  },

  // 生命周期 - 创建完成（可以访问当前this 实例）
  created() {
  },
  // 生命周期 - 挂载完成（可以访问 DOM 元素）
  mounted() {
    this.getList();
  },
  activated(){
    this.getList();
  }

}
</script>

<style lang="scss" scoped>
</style>
